package com.shaco.heroproject.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.shaco.heroproject.entity.Result_LatestRecomm;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/11/11.
 */
public class MySQLiteLatestNewsPageUtil {
    private SQLiteDatabase db;
    private Context context;
    public MySQLiteLatestNewsPageUtil(Context c) {
        // TODO Auto-generated constructor stub
        context = c;
        init();
    }

    private void init() {
        MySQLliteOpenHelper helper = new MySQLliteOpenHelper(context, Constant.DATABASE_NAME, null, 1);
        db = helper.getWritableDatabase();
    }
    public void saveDatasToDataBase(List<Result_LatestRecomm> results, String type) {
        for (Result_LatestRecomm result : results) {
            ContentValues values = new ContentValues();
            values.put(Constant.COLUMN_LATESTNEWSPAGE_ARTICLE_ID, result.getArticle_id());
            values.put(Constant.COLUMN_LATESTNEWSPAGE_BAN_IMG,result.getBan_img());
            values.put(Constant.COLUMN_LATESTNEWSPAGE_NAME,result.getName());
            values.put(Constant.COLUMN_LATESTNEWSPAGE_TYPE,type);
            db.insert(Constant.TABLE_NAME_LATESTNEWSPAGE, null, values);
        }
    }
    public List<Result_LatestRecomm> queryByType(String type) {
        String sql = " SELECT * FROM " + Constant.TABLE_NAME_LATESTNEWSPAGE;
        Cursor cursor = db.rawQuery(sql, null);
        if (cursor != null) {
            List<Result_LatestRecomm> results = new ArrayList<Result_LatestRecomm>();
            while (cursor.moveToNext()) {
                int comment_count=cursor.getInt(cursor.getColumnIndex(Constant.COLUMN_LATESTNEWS_COMMENT_COUNT));
                String article_id=cursor.getString(cursor.getColumnIndex(Constant.COLUMN_LATESTNEWSPAGE_ARTICLE_ID));
                String ban_img=cursor.getString(cursor.getColumnIndex(Constant.COLUMN_LATESTNEWSPAGE_BAN_IMG));
                String name=cursor.getString(cursor.getColumnIndex(Constant.COLUMN_LATESTNEWSPAGE_NAME));

                Result_LatestRecomm result = new Result_LatestRecomm();
                result.setComment_count(comment_count);
                result.setArticle_id(article_id);
                result.setBan_img(ban_img);
                result.setName(name);
                results.add(result);
            }
            cursor.close();
            return results;
        }
        return null;
    }
    public void deleteByTitle(int id) {
        String sql = " DELETE  FROM " + Constant.TABLE_NAME_ALLHERORESULT + " WHERE id = " + id;
        db.execSQL(sql);
    }

    public void deleteAll() {
        String sql = " DELETE FROM " + Constant.TABLE_NAME_ALLHERORESULT;
        db.execSQL(sql);
    }

}
